package com.aizhixin.test.question.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import com.aizhixin.test.common.PageJdbcUtil;
import com.aizhixin.test.question.dto.QuestionDTO;
import com.aizhixin.test.question.dto.QuestionOptionDTO;
import com.aizhixin.test.question.dto.judgeQuestionNeedDTO;


@Component
public class QuestionDAO {
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private PageJdbcUtil pageJdbcUtil;
	@Autowired
	private QuestionOptionDAO questionOptionDao;
	/*
	 * 通过抽到的题来返回题目
	 */
	public QuestionDTO  findById1 (int x) {
		final String sql = "SELECT q.ID, q.CONTENT, q.ANSWER,q.KNOWLEDGE_AREA FROM  t_question q WHERE q.ID = ?";	
		List<QuestionDTO> list = jdbcTemplate.query(sql, 
				new Object []{x}, 
				new int[] {Types.INTEGER},
				new RowMapper<QuestionDTO >() {//行映射
	            public QuestionDTO  mapRow(ResultSet rs, 
	            		int rowNum) throws SQLException {
	            	QuestionDTO test = new QuestionDTO ();
	            	test.setId(rs.getLong("ID"));
	            	test.setContent(rs.getString("CONTENT"));
	            	test.setAnswer(rs.getString("ANSWER"));
	            	test.setKnowledgeArea(rs.getString("KNOWLEDGE_AREA"));
//	            	test.setOptions(a);
	                return test;
	            }
	        });

		if (null != list && list.size() > 0) {
			QuestionDTO questionDTO = list.get(0);
			questionDTO.setOptions(questionOptionDao.findByQuestionId(x));
			return questionDTO;
		}
		else {
			return null;
		}
		
	}
	public int countAll() {
		final String sql="SELECT COUNT(*) FROM t_question";
		int c=jdbcTemplate.queryForObject(sql, Integer.class);
		return c;
	}
	
		
	}


